package com.sjsu.cloudmanager.client.ui;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.binding.SimpleComboBoxFieldBinding;
import com.extjs.gxt.ui.client.data.BaseListLoadResult;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.*;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.*;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sjsu.cloudmanager.client.CloudManager;
import com.sjsu.cloudmanager.client.LabManagerEvents;
import com.sjsu.cloudmanager.client.service.CloudLabServiceAsync;
import com.sjsu.cloudmanager.client.ui.model.CloudLabBean;
import com.sjsu.cloudmanager.client.ui.model.ComputingMachineBean;
import com.sjsu.cloudmanager.client.ui.model.SoftwareBean;
import com.sjsu.cloudmanager.model.ComputingMachine;

import java.util.Arrays;
import java.util.List;

/**
 * Lab resource panel
 * <p/>
 * User: ckempaiah
 * Date: 10/4/11
 * Time: 9:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class LabResourceFormPanel extends Dialog {

    FormBinding formBinding;
    FormPanel formPanel;

    @Override
    protected void onRender(Element parent, int index) {
        super.onRender(parent, index);
        setButtons("");
        setIcon(IconHelper.createStyle("user"));
        setHeading("Lab Manager Add Machine");
        setModal(true);
        setBodyBorder(false);
        setLayout(new FormLayout());
        setWidth(450);
        //setBodyBorder(true);
        //setWidth(300);
        FormPanel panel = new FormPanel();
        panel.setHeaderVisible(false);
        TextField<String> name = new TextField<String>();
        name.setName("machineName");
        name.setAllowBlank(false);
        name.setFieldLabel("Resource Name");
        panel.add(name);

//        TextField<String> machineCloudId = new TextField<String>();
//        machineCloudId.setName("machineCloudId");
//        machineCloudId.setFieldLabel("Resource Cloud Id");
//        machineCloudId.setReadOnly(true);
//        panel.add(machineCloudId);

        SimpleComboBox<String> platform = new SimpleComboBox<String>();
        platform.setName("operatingSystem");
        platform.setEmptyText("Select a Platform");
        platform.setAllowBlank(false);
        platform.setFieldLabel("Platform");
        platform.add(Arrays.asList(new String[]{"Linux 7.0", "Unix", "Windows 7 64 bit", "Windows 7 32 bit"}));
        panel.add(platform);

        NumberField memory = new NumberField();
        memory.setName("memory");
        memory.setAllowBlank(false);
        memory.setFieldLabel("Memory");
        memory.setPropertyEditorType(Integer.class);
        panel.add(memory);

        HiddenField<Integer> machineId = new HiddenField<Integer>();
        machineId.setName("machineId");
        panel.add(machineId);

        //Combo box for lab
        ComboBox<CloudLabBean> cloudLabComboBox = new ComboBox<CloudLabBean>();
        BaseListLoader<BaseListLoadResult<ModelData>> listLoader = getLoader();
        cloudLabComboBox.setStore(new ListStore<CloudLabBean>(listLoader));
        cloudLabComboBox.setFieldLabel("Lab");
        cloudLabComboBox.setName("cloudLabBean");
        cloudLabComboBox.setDisplayField("labName");
        cloudLabComboBox.setValueField("cloudLabBean");
        cloudLabComboBox.setAllowBlank(false);
        //cloudLabComboBox.setTriggerAction(ComboBox.TriggerAction.ALL);
        panel.add(cloudLabComboBox);

        //Software combobox
//        final DualListField<SoftwareBean> lists = new DualListField<SoftwareBean>();
//        lists.setMode(DualListField.Mode.INSERT);
//        lists.setFieldLabel("Software");
//
//        ListField<SoftwareBean> from = lists.getFromList();
//        from.setDisplayField("softwareName");
//        from.setValueField("softwareId");
//        ListStore<SoftwareBean> store = new ListStore<SoftwareBean>();
//        store.add(new SoftwareBean(1, "jdk 1.5"));
//        from.setStore(store);
//        ListField<SoftwareBean> to = lists.getToList();
//        to.setDisplayField("softwareName");
//        to.setValueField("softwareId");
//        store = new ListStore<SoftwareBean>();
//        to.setStore(store);
//        panel.add(lists);
        final LayoutContainer container = this;
        Button save = new Button("Save");
        Button cancel = new Button("Cancel");
        cancel.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                container.hide();
            }
        });

        save.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent buttonEvent) {
                if (formPanel.isValid()) {

                    final CloudLabServiceAsync service = (CloudLabServiceAsync) Registry.get(CloudManager.CLOUD_LAB_SERVICE);
                    service.saveCloudResource((ComputingMachineBean) formBinding.getModel(), new AsyncCallback<Void>() {
                        @Override
                        public void onFailure(Throwable throwable) {
                            MessageBox.alert(MessageBox.ERROR, "Failed To Save ", null);
                        }

                        @Override
                        public void onSuccess(Void aVoid) {
                            MessageBox.alert(MessageBox.INFO, "Successfully Saved", null);

                            container.hide();
                            ToolbarAction.getInstance().fireEvent(LabManagerEvents.SaveMachine, new BaseEvent(LabManagerEvents.SaveMachine));
                        }
                    });
                }
            }
        });
        panel.addButton(save);
        panel.addButton(cancel);

        FormButtonBinding fbb = new FormButtonBinding(panel);

        fbb.addButton(save);
        panel.setButtonAlign(Style.HorizontalAlignment.CENTER);
        formPanel = panel;
        formBinding = new FormBinding(panel, true);
        formBinding.addFieldBinding(new SimpleComboBoxFieldBinding(platform, "operatingSystem"));
        add(panel, new FitData());
    }


    private BaseListLoader<BaseListLoadResult<ModelData>> getLoader() {
        final CloudLabServiceAsync service = (CloudLabServiceAsync) Registry.get(CloudManager.CLOUD_LAB_SERVICE);

        RpcProxy<List<CloudLabBean>> proxy = new RpcProxy<java.util.List<com.sjsu.cloudmanager.client.ui.model.CloudLabBean>>() {
            @Override
            public void load(Object loadConfig,
                             AsyncCallback<java.util.List<com.sjsu.cloudmanager.client.ui.model.CloudLabBean>> callback) {
                service.getCloudLabBeanList(callback);
            }
        };

        final BaseListLoader<BaseListLoadResult<ModelData>> loader = new BaseListLoader<BaseListLoadResult<ModelData>>(proxy);
        return loader;
    }

    public FormBinding getFormBinding() {
        return formBinding;
    }

    public FormPanel getFormPanel() {
        return formPanel;
    }

}
